var BlogCon = iClass.set("Blog", function(info) {
	this._id = BlogCon.getUuid();

	this.setInfo(info);

	//观点
	this.viewpoint = [];
	this.user_id = info.user_id;
	this.create_time = +new Date;

	db.insert(this.dbname, this);
	midEvent.emit("new_blog", this._id);
});
var UserCon = iClass.get("User");
db.find_all(BlogCon.dbname).forEach(function(blog_info) {
	var blog = BlogCon.getInstance(blog_info);
	if (!blog.type) {
		blog.type = "blog";
	}
	blog.save();
});
//获取开放的博文
BlogCon.getOpenList = function(visitor, type, blog_key, num, page) {
	var blog_list = db.find_all(BlogCon.dbname).filterMap(function(blog_info) {
		var blog = BlogCon.getInstance(blog_info);
		if (blog.isOpenForSomeUser(visitor, blog_key)) {
			if (type) {
				return blog.type == type && blog;
			} else {
				return blog;
			}
		}
	});
	//处理参数
	var total_num = blog_list.length;
	num = ~~num;
	num > 0 || (num = total_num); //num > 0
	page = ~~page;
	page >= 0 || (page = 0); //page >= 0
	var total_page = Math.ceil(total_num / num) || 1;
	//整理数据
	blog_list = blog_list.reverse().page(num, page).map(function(blog) {
		blog = blog.clone();
		//作者详情
		blog.user = UserCon.get(blog.user_id);
		return blog;
	});
	return {
		total_num: total_num,
		total_page: total_page,
		num: num,
		page: page,
		list: blog_list
	};
};
var TagCon = iClass.get("Tag");
BlogCon.getOpenListByTag = function(tag_id, visitor, type, blog_key, num, page) {
	if (!TagCon.get(tag_id)) {
		Throw("找不到指定标签")
	}
	var blog_list = db.find_all(BlogCon.dbname).filterMap(function(blog_info) {
		var blog = BlogCon.getInstance(blog_info);
		if (blog.hasTag(tag_id) && blog.isOpenForSomeUser(visitor, blog_key)) {
			if (type) {
				return blog.type == type && blog;
			} else {
				return blog;
			}
		}
	});
	//处理参数
	var total_num = blog_list.length;
	num = ~~num;
	num > 0 || (num = total_num); //num > 0
	page = ~~page;
	page >= 0 || (page = 0); //page >= 0
	var total_page = Math.ceil(total_num / num) || 1;
	//整理数据
	blog_list = blog_list.reverse().page(num, page).map(function(blog) {
		blog = blog.clone();
		//作者详情
		blog.user = UserCon.get(blog.user_id);
		return blog;
	});;
	return {
		total_num: total_num,
		total_page: total_page,
		num: num,
		page: page,
		list: blog_list
	};
};